<script lang="ts" defer async setup>
import { onMounted, reactive } from 'vue'
import { changePwd } from '@/api/login'
import { useI18n } from 'vue-i18n'
import { useSwipeHandlers } from '@/utils/useSwipeHandlers'
import eyeOpen from '@/static/img/eye-open.png'
import eyeClose from '@/static/img/eye-close.png'
const { t } = useI18n()

const baseData = reactive({
  showCountDown: false,
  showPassWord: false,
  showOldPassWord: false,
  showcPassWord: false,
  secounds: 60,
  current: 0,
  registerForm: {
    email: '',
    password: null,
    oldpassword: null,
    code: null,
    confirmPassword: null,
    source: null,
  },
})

const rightClick = () => {
		window.top?.postMessage('/pages/user/index', window.location.origin)
		uni.navigateTo({
			url: '/pages/user/index',
		})
}

const bind = async () => {
  if (baseData.registerForm.password != baseData.registerForm.confirmPassword) {
    uni.showToast({
      title: t('Passwords Do Not Match'),
      icon: 'none',
    })
    return
  }
  
  if (!baseData.registerForm.password) {
    uni.showToast({
      title: t('Password Cannot Be Empty'),
      icon: 'none',
    })
    return
  }
  
  if (!baseData.registerForm.oldpassword) {
    uni.showToast({
      title: t('Please enter your old password'),
      icon: 'none',
    })
    return
  }
  
  let params: any = {
    password: baseData.registerForm.oldpassword,
    newPassword: baseData.registerForm.password,
  }
  
  try {
    const res = await changePwd(params)
    if (res.code === 0) {
      uni.showToast({
        title: res.msg,
        icon: 'none',
      })
      setTimeout(() => {
        rightClick()
      }, 1000)
    } else {
      uni.showToast({
        title: res.msg,
        icon: 'none',
      })
    }
  } catch (error) {
    console.error('Failed to change password:', error)
    uni.showToast({
      title: t('Failed to change password'),
      icon: 'none',
    })
  }
}


const handleSwipeRight = () => {
  const pages = getCurrentPages()
  if (pages.length <= 1) {
    history.back()
  } else {
    uni.navigateBack()
  }
}

useSwipeHandlers(handleSwipeRight)

onMounted(() => {
  
  uni.setNavigationBarTitle({
    title: 'Studio ' + t('Earn money so easy') + '!', 
  })
})
</script>

<template>
  <view class="modify-password-container">

		<up-navbar :title="t('Modify Password')" @click="rightClick" :autoBack="true">
						<template #left>
						  <view class="nav-back-btn">
						    <image class="back-icon" src="../../../static/img/left.png"></image>
						  </view>
						</template>
					</up-navbar>
    
    <view class="header-section">
      <view class="title-section">
        <text class="main-title">{{t('Modify your Password')}}</text>
      </view>
      <view class="laptop-3d">
        <view class="laptop-body">
          <view class="laptop-screen"></view>
          <view class="laptop-keyboard"></view>
        </view>
        <view class="padlock-icon">
          <view class="lock-body"></view>
          <view class="lock-shackle"></view>
          <view class="keyhole"></view>
        </view>
      </view>
    </view>

    
    <view class="form-section">
      
      <view class="input-group">
        <input 
          v-model="baseData.registerForm.oldpassword" 
          class="form-input" 
          :type="baseData.showOldPassWord ? 'text' : 'password'"
          :placeholder="t('Old Password')"
        />
        <view class="password-toggle" @click="baseData.showOldPassWord = !baseData.showOldPassWord">
           <img class="toggle-icon" :src="baseData.showOldPassWord ? eyeOpen : eyeClose" mode="aspectFit"/>
        </view>
      </view>

      
      <view class="input-group">
        <input 
          v-model="baseData.registerForm.password" 
          class="form-input" 
          :type="baseData.showPassWord ? 'text' : 'password'"
          :placeholder="t('New Password')"
        />
        <view class="password-toggle" @click="baseData.showPassWord = !baseData.showPassWord">
         <img class="toggle-icon" :src="baseData.showPassWord ? eyeOpen : eyeClose" mode="aspectFit"/>
        </view>
      </view>

      
      <view class="input-group">
        <input 
          v-model="baseData.registerForm.confirmPassword" 
          class="form-input" 
          :type="baseData.showcPassWord ? 'text' : 'password'"
          :placeholder="t('Confirm Login Password')"
        />
        <view class="password-toggle" @click="baseData.showcPassWord = !baseData.showcPassWord">
           <img class="toggle-icon" :src="baseData.showcPassWord ? eyeOpen : eyeClose" mode="aspectFit"/>
        </view>
      </view>
    </view>

    
    <view class="submit-section">
      <view class="submit-button" @click="bind">
        <text class="submit-text">{{t('Submit')}}</text>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.modify-password-container {
  min-height: 100vh;
  background: linear-gradient(180deg, #e8f5e8 0%, #ffffff 100%);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  position: relative;
}



.header-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 40px 30px 30px;
  padding-top: 70px;
  
  .title-section {
    flex: 1;
    
    .main-title {
      display: block;
      font-size: 28px;
      font-weight: 900;
      color: #000;
      line-height: 1.1;
      letter-spacing: -0.5px;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }
  }
  
  .laptop-3d {
    width: 120px;
    height: 80px;
    position: relative;
    
    .laptop-body {
      width: 100%;
      height: 60px;
      background: linear-gradient(135deg, #87CEEB 0%, #4682B4 100%);
      border-radius: 8px 8px 4px 4px;
      position: relative;
      box-shadow: 0 8px 20px rgba(135, 206, 235, 0.3);
      transform: perspective(1000px) rotateY(-15deg) rotateX(5deg);
      
      .laptop-screen {
        position: absolute;
        top: 8px;
        left: 8px;
        right: 8px;
        height: 35px;
        background: linear-gradient(135deg, #E6F3FF 0%, #B3D9FF 100%);
        border-radius: 4px;
        border: 1px solid rgba(255, 255, 255, 0.3);
      }
      
      .laptop-keyboard {
        position: absolute;
        bottom: 4px;
        left: 8px;
        right: 8px;
        height: 8px;
        background: rgba(0, 0, 0, 0.1);
        border-radius: 2px;
      }
    }
    
    .padlock-icon {
      position: absolute;
      top: -10px;
      right: 10px;
      width: 30px;
      height: 30px;
      
      .lock-body {
        width: 20px;
        height: 20px;
        background: linear-gradient(135deg, #4169E1 0%, #1E90FF 100%);
        border-radius: 4px;
        position: relative;
        box-shadow: 0 4px 12px rgba(65, 105, 225, 0.4);
        
        &::before {
          content: '';
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 6px;
          height: 6px;
          background: #fff;
          border-radius: 50%;
        }
      }
      
      .lock-shackle {
        position: absolute;
        top: -8px;
        left: 50%;
        transform: translateX(-50%);
        width: 16px;
        height: 12px;
        border: 3px solid #4169E1;
        border-bottom: none;
        border-radius: 8px 8px 0 0;
        background: transparent;
      }
      
      .keyhole {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 4px;
        height: 4px;
        background: #fff;
        border-radius: 50%;
      }
    }
  }
}

.form-section {
  padding: 0 30px;
  
  .input-group {
    margin-bottom: 25px;
    position: relative;
    
    .form-input {
      width: 100%;
      height: 50px;
      padding: 0 50px 0 15px;
      border: 1px solid #E0E0E0;
      border-radius: 8px;
      background: #fff;
      font-size: 16px;
      color: #333;
      box-sizing: border-box;
      

    }
    
    .password-toggle {
      position: absolute;
      right: 15px;
      top: 50%;
      transform: translateY(-50%);
      width: 30px;
      height: 30px;
      display: flex;
      align-items: center;
      justify-content: center;
	    .toggle-icon {
	                width: 24px;
	                height: 24px;
	               
	              }
    }
  }
}

.submit-section {
  padding: 30px;
  
  .submit-button {
    width: 100%;
    height: 50px;
    background: linear-gradient(90deg, #4CAF50 0%, #8BC34A 100%);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
    
    .submit-text {
      font-size: 18px;
      font-weight: bold;
      color: #000;
    }
  }
}
</style>